package com.example.administrator.readbook.ui.fragment;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.readbook.R;

/**
 * Created by Administrator on 2018/10/17.
 */

@SuppressLint("ValidFragment")
public class ReadBookDialogFragment extends DialogFragment {
    private String title;
    public readDialogInterface readDialogInterface;
    private ImageView nightModel;
    private boolean isNight = false;
    private int xLeft, xRight, yTop, yBottom;

    public static ReadBookDialogFragment getInstance(String title, ReadBookDialogFragment.readDialogInterface readDialogInterface,int xLeft,int xRight,int yTop,int yBottom) {
        ReadBookDialogFragment dialogFragment = new ReadBookDialogFragment(title, readDialogInterface, xLeft, xRight, yTop, yBottom);
        return dialogFragment;
    }

    public ReadBookDialogFragment(String title, ReadBookDialogFragment.readDialogInterface readDialogInterface,int xLeft,int xRight,int yTop,int yBottom) {
        this.title = title;
        this.readDialogInterface = readDialogInterface;
        this.xLeft = xLeft;
        this.xRight = xRight;
        this.yTop = yTop;
        this.yBottom = yBottom;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        /*View view = inflater.inflate(R.layout.dialog_readbook, null);
        ImageView backImg = view.findViewById(R.id.dialog_readbook_back);
        TextView titleTv = view.findViewById(R.id.dialog_readbook_title);
        ImageView nightModel = view.findViewById(R.id.dialog_readbook_nightmodel);

        titleTv.setText(title);

        backImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        nightModel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isMoon) {
                    isMoon = false;
                } else {
                    isMoon = true;
                }
                readDialogInterface.changeModel(isMoon);
            }
        });
        return view;*/
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_readbook, null);
        ImageView backImg = view.findViewById(R.id.dialog_readbook_back);
        TextView titleTv = view.findViewById(R.id.dialog_readbook_title);
        nightModel = view.findViewById(R.id.dialog_readbook_nightmodel);
        final Dialog dialog = new Dialog(getActivity(), R.style.Theme_AppCompat_Dialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);
        Window window = dialog.getWindow();
        window.setBackgroundDrawableResource(R.color.nothing);
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        wlp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(wlp);

        backImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        nightModel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isNight) {
                    readDialogInterface.changeModel(true);
                    isNight = false;
                } else {
                    readDialogInterface.changeModel(false);
                    isNight = true;
                }
            }
        });
        titleTv.setText(title);

        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        if (event.getX() > xLeft && event.getX() < xRight && event.getY() > yTop && event.getY() < yBottom) {
                            dialog.dismiss();
                        }
                        break;
                }
                return false;
            }
        });

        return dialog;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        readDialogInterface.dialogDismiss();
    }

    public void setNightModelImg(int res) {
        if (nightModel != null) {
            nightModel.setImageResource(res);
        }
    }

    public interface readDialogInterface {
        void changeModel(boolean isNight);

        void dialogDismiss();
    }

}
